moddbfandomcom-20200213-history
Java:Tutorials:Java3D
= Using Java3D to create a 3D game world = Java3D is a Java library that provides classes to greatly simplify the use of 3D graphics. The Universe The main class that holds and displays 3D objects is called a Universe, these can(apparantely) span for light years of pixels. The SimpleUniverse class is the easiest to use, and will be used in the examples. This code will set up a SimpleUniverse import com.sun.j3d.utils.universe.SimpleUniverse; import javax.media.j3d.BranchGroup; public class Test3D{ SimpleUniverse universe; BranchGroup group; public Test3D(){ universe = new SimpleUniverse(); group = new BranchGroup(); universe.addBranchGraph(group); } } The BranchGroup class used above is a collection of objects within the universe. It uses the addChild(Node child) to add objects to the collection. The main Node sub-class you will use is called Shape3D, this can be used to create any kind of 3D shape, however, there are some convenience classes that are predefined shapes, for the example we will use the Sphere class. import com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.geometry.Sphere; import javax.media.j3d.BranchGroup; public class Test3D{ SimpleUniverse universe; BranchGroup group; Sphere sphere; public Test3D(){ universe = new SimpleUniverse(); group = new BranchGroup(); sphere = new Sphere(0.5f); group.addChild(sphere); universe.getViewingPlatform().setNominalViewingTransform(); universe.addBranchGraph(group); } } Adding a 3D Object The universe will now contain a Sphere object with radius 0.5 (notice the Sphere constructor contains a float value of 0.5). The method universe.getViewingPlatform().setNominalViewingTransform() is then used to move back the view slightly so the Sphere could be viewed. Using Light Now in order to see the fruits of our work, we will need some light in our universe. I prefer to use the DirectionalLight class for this, you may find changing the direction a bit of a hassle as soon as you have implemented movement into your game, however the AmbientLight class which lights up everywhere, makes your objects look single coloured and barely 3-dimensional. The thing that makes using light more complicated is that your objects need to know how to reflect the light in order to define their colour. This is done with the Appearance class that will be assigned to the Sphere. import com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.geometry.Sphere; import javax.media.j3d.BranchGroup; import javax.vecmath.Color3f; import javax.media.j3d.Appearance; import javax.media.j3d.Material; import javax.media.j3d.DirectionalLight; public class Test3D{ SimpleUniverse universe; BranchGroup group; Sphere sphere; DirectionalLight light; public Test3D(){ universe = new SimpleUniverse(); group = new BranchGroup(); sphere = new Sphere(0.5f); sphere.setCapability(Sphere.ALLOW_APPEARANCE_WRITE); sphere.setAppearance(createAppearance()); group.addChild(sphere); light = new DirectionalLight(); group.addChild(light); universe.getViewingPlatform().setNominalViewingTransform(); universe.addBranchGraph(group); } //Returns an Appearance class that will appear red under DirectionalLight public Appearance createAppearance(){ Appearance app = new Appearance(); app.setCapability(Appearance.ALLOW_MATERIAL_WRITE); Material mat = new Material(); mat.setCapability(Material.ALLOW_COMPONENT_WRITE); mat.setDiffuseColor(new Color3f(1.0f,0.0f,0.0f)); mat.setSpecularColor(new Color3f(1.0f,0.0f,0.0f)); app.setMaterial(mat); return app; } } Notice how the DirectionalLight object is also added to the BranchGroup. The setting up of the Appearance used a class called Material, this contains the Color3f values for different types of light (Diffuse and Specular). This tells the Appearance class which components of that light is reflected to be viewed, in this cases the red component was set to 1.0 and the blue and green components were both set to 0.0. This will make the object appear red. The setCapability() method is used a lot in the Java3D API but is quite easy to get used to, its use is self-explanatory, it basically lets you allow changing and reading of variables within an object, so when not allowed they can be protected from accidental change. Although there is a probably a deeper purpose for it than this. = External Links = *Download Java3D *Java3D API documentation Category:Java